package com.aaa.rabbitmq.five;

import com.aaa.rabbitmq.one.util.RabbitMQUtil;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.DeliverCallback;

import java.nio.charset.StandardCharsets;

/**
 *  发布订阅模式 （fanout）
 * 消息的接收
 */
public class ReceiveLogs01 {

    //交换机的名称
    public static final String EXCHANGE_NAME = "logs";

    public static void main(String[] args) throws Exception {
        Channel channel = RabbitMQUtil.getChannel();
        //声明一个交换机
        channel.exchangeDeclare(EXCHANGE_NAME,"fanout");
        //声明一个临时队列（名字是随机的） 消费者断开与队列的连接时 将自动删除
        String queue = channel.queueDeclare().getQueue();
        //交换机和队列绑定
        channel.queueBind(queue,EXCHANGE_NAME,"");
        System.out.println("ReceiveLogs01：等待接受消息，把接收到的消息打印");
        //接受消息的回调
        DeliverCallback callback = (tag,message )->{
            System.out.println("ReceiveLogs01：打印收到的消息"+new String(message.getBody(), StandardCharsets.UTF_8));
        };
        //消费消息
        channel.basicConsume(queue,true,callback,consumerTag ->{});




    }
}
